﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using UeyesAIS.DataDefinition;
using UeyesAIS.Interface;

namespace UeyesAIS.Service.OpticStrategy
{
    /// <summary>
    /// 更改光学检测方案时重写此类，实现IStrategy接口
    /// </summary>
    internal class Strategy0085 : IStrategy<DeviceParameter, ProjectParam,BasePicParameter>
    {
        public Dictionary<string, bool> DitUseVirtual { get; set; }

        public IDeviceService<DeviceParameter, ProjectParam> DeviceSvr { get; }

        public Strategy0085(IDeviceService<DeviceParameter, ProjectParam> device)
        {
            DeviceSvr = device;
            DitUseVirtual = new Dictionary<string, bool>();
        }

        public bool ItemTaskBefore(BasePicParameter param)
        {
            if (DitUseVirtual.All(x => x.Value))
                return true;
            var index = param.Picindex;
            var res = true;
            switch (index)
            {
                case -3://上清灰,开上测光
                    DeviceSvr.DeviceEntityControl.OpenUpSideLight((byte)param.Intensity);
                    break;
                case -2://下清灰，开下侧光
                    DeviceSvr.DeviceEntityControl.OpenDownSideLight((byte)param.Intensity);
                    break;
                case -1://目测，开背光
                    DeviceSvr.DeviceEntityControl.OpenBackLight((byte)param.Intensity);
                    break;
                default://电测图，切图
                    DeviceSvr.DeviceEntityControl.OpenBackLight((byte)param.Intensity);
                    DeviceSvr.DeviceEntityControl.ChangeColor(param.ETSPicIndex);
                    Thread.Sleep(param.ChangeColorTime);
                    break;
            }
            return res;
        }

        public void ItemTaskFinished(BasePicParameter param)
        {
            var index = param.Picindex;
            switch (index)
            {
                case -3://上清灰
                    DeviceSvr.DeviceEntityControl.CloseUpSideLight();
                    break;
                case -2://下清灰
                    DeviceSvr.DeviceEntityControl.CloseDownSideLight();
                    break;
                default://切完图，关背光
                    DeviceSvr.DeviceEntityControl.CloseBackLight();
                    break;
            }
        }

        public void ProcessStart(string id)
        {
            //ImageManage.Instance.Refrush(0);
        }

        public void ProcessFinished()
        {
            if (DitUseVirtual.All(x => x.Value))
                return;
            DeviceSvr.DeviceEntityControl.End();
        }

        public bool ElectricReady()
        {
            if (DitUseVirtual.All(x => x.Value))
                return true;
            var res = DeviceSvr.DeviceEntityControl.Begin();
            return res;
        }
    }
}
